<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="type_name" label="分类名称" required>
        <uni-easyinput placeholder="请输入分类名称，2-10位组成" v-model="formData.type_name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="type_description" label="分类描述">
        <uni-easyinput type="textarea" :maxlength="300" placeholder="请输入分类描述（选填）" v-model="formData.type_description" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="type_sort" label="分类排行">
        <uni-easyinput placeholder="值越大越往后" type="number" v-model="formData.type_sort"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="type_status" label="启用或关闭">
        <switch style="transform: scale(0.8);" @change="binddata('type_status', $event.detail.value)" :checked="formData.type_status"></switch>
      </uni-forms-item>
      <uni-forms-item >
        <lay-form-item  class="_row_style" style="margin-top:10px">
          <lay-button @click="submit" type="normal">确认提交</lay-button>
          <lay-button @click="closeAll">取消关闭</lay-button>
        </lay-form-item>
      </uni-forms-item>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/food-store-types.js';
  import {store} from '@/uni_modules/uni-id-pages/common/store.js'

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'food-store-types';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "merchant_id": store.userInfo.merchant_id,
        "type_name": "",
        "type_description": "",
        "type_sort": 1,
        "type_status": true
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    mounted() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      closeAll(){
        this.$emit(`manualClose`,`addComponent`)
      },


      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          res.merchant_id=store.userInfo.merchant_id;
          
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.$emit(`refreshData`)
          this.closeAll()
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
